from pydantic import BaseModel, Field
from typing import Optional, List, Dict, Any
from datetime import datetime


class DataPermissionRuleResponse(BaseModel):
    """数据权限规则响应模型"""
    id: Optional[int] = Field(None, description="规则ID")
    name: str = Field(..., description="规则名称")
    rule_code: str = Field(..., description="规则编码")
    table_name: str = Field(..., description="目标表名")
    logical_operator: str = Field(..., description="多条件组合方式: AND/OR")
    condition_type: str = Field(..., description="条件类型：eq,in,like,between等")
    config_json: Dict[str, Any] = Field(..., description="权限配置JSON")
    description: Optional[str] = Field(None, description="规则描述")
    status: Optional[bool] = Field(True, description="是否启用")
    create_time: Optional[datetime] = Field(None, description="创建时间")
    update_time: Optional[datetime] = Field(None, description="更新时间")

    class Config:
        from_attributes = True


class DataPermissionRuleListResponse(BaseModel):
    """数据权限规则列表响应模型"""
    total: int = Field(..., description="总数量")
    items: List[DataPermissionRuleResponse] = Field(..., description="规则列表")
    page: int = Field(..., description="页码")
    size: int = Field(..., description="每页数量")
    pages: int = Field(..., description="总页数")


class RoleDataPermissionResponse(BaseModel):
    """角色数据权限关联响应模型"""
    id: Optional[int] = Field(None, description="关联ID")
    role_id: int = Field(..., description="角色ID")
    rule_id: int = Field(..., description="规则ID")
    status: Optional[bool] = Field(True, description="是否启用")
    create_time: Optional[datetime] = Field(None, description="创建时间")
    update_time: Optional[datetime] = Field(None, description="更新时间")
    
    # 关联信息
    rule_name: Optional[str] = Field(None, description="规则名称")
    rule_code: Optional[str] = Field(None, description="规则编码")

    class Config:
        from_attributes = True


class RoleDataPermissionListResponse(BaseModel):
    """角色数据权限关联列表响应模型"""
    total: int = Field(..., description="总数量")
    items: List[RoleDataPermissionResponse] = Field(..., description="关联列表")
    page: int = Field(..., description="页码")
    size: int = Field(..., description="每页数量")
    pages: int = Field(..., description="总页数")


class RoleDataPermissionRulesResponse(BaseModel):
    """角色数据权限规则响应模型"""
    role_id: int = Field(..., description="角色ID")
    rule_ids: List[int] = Field(default_factory=list, description="规则ID列表")